package bb.lanxing.model.map;

import android.os.Parcel;
import android.os.Parcelable;
import bb.lanxing.lib.kt.model.geo.IGeoPoint;

public class GeoBounds implements Parcelable {
    public static final Parcelable.Creator<GeoBounds> CREATOR = new Parcelable.Creator<GeoBounds>() {
        public GeoBounds createFromParcel(Parcel source) {
            return new GeoBounds(source);
        }

        public GeoBounds[] newArray(int size) {
            return new GeoBounds[size];
        }
    };
    public final double east;
    public final double north;
    public final double south;
    private int type;
    public final double west;

    @Override
    public int describeContents() {
        return 0;
    }

    public GeoBounds(int type, double north, double east, double south, double west) {
        this.type = type;
        this.north = north;
        this.east = east;
        this.south = south;
        this.west = west;
    }

    public boolean contain(IGeoPoint point) {
        if (point == null) {
            return false;
        }
        double latitude = point.getLatitude();
        double longitude = point.getLongitude();
        return latitude >= this.south && latitude <= this.north && longitude >= this.west && longitude <= this.east;
    }

    public IGeoPoint getCenter() {
        return new GeoPoint(IGeoPoint.TYPE_EARTH, (this.north + this.south) / 2.0d, (this.east + this.west) / 2.0d);
    }

    public int getType() {
        return this.type;
    }

    public GeoBounds toType(int type) {
        if (type == this.type) {
            return this;
        }
        IGeoPoint type2 = new GeoPoint(this.type, this.north, this.east).toType(type);
        IGeoPoint type3 = new GeoPoint(this.type, this.south, this.west).toType(type);
        return new GeoBounds(type, type2.getLatitude(), type2.getLongitude(), type3.getLatitude(), type3.getLongitude());
    }

    public static class Builder {
        private double east;
        private boolean first = true;
        private double north;
        private double south;
        private double west;

        public void include(double lat, double lng) {
            if (this.first) {
                this.south = lat;
                this.north = lat;
                this.west = lng;
                this.east = lng;
                this.first = false;
            }
            if (lat > this.north) {
                this.north = lat;
            }
            if (lat < this.south) {
                this.south = lat;
            }
            if (lng < this.west) {
                this.west = lng;
            }
            if (lng > this.east) {
                this.east = lng;
            }
        }

        public GeoBounds build(int type) {
            return new GeoBounds(type, this.north, this.east, this.south, this.west);
        }
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeDouble(this.north);
        dest.writeDouble(this.east);
        dest.writeDouble(this.south);
        dest.writeDouble(this.west);
    }

    protected GeoBounds(Parcel in2) {
        this.north = in2.readDouble();
        this.east = in2.readDouble();
        this.south = in2.readDouble();
        this.west = in2.readDouble();
    }
}